<template>
  <base-breadcrumb>
    <searchData :search-data="searchArr" @search="search" />
    <a-spin :spinning="progressing">
      <a-card :bordered="false" style="over-flow: scroll">
        <div>
          <a-row>
            <a-col :md="24" :sm="19">
              <div class="table-operator">
                <a-row>
                  <a-col :md="24" :sm="24">
                    <a-button v-btnPermission="'pm_016_add_01'" type="primary" @click="goAdd('add')">
                      新增考察
                    </a-button>
                  </a-col>
                </a-row>
              </div>

              <div class="pub-table-select-count">
                <a-icon type="info-circle" theme="twoTone" />
                <span style="margin-right: 12px">
                  分包商数：<span class="totalNum">{{ total || 0 }} 个 </span>
                </span>
              </div>
              <list-table ref="table" size="default" row-key="row.id" :columns="columns" :request="loadData" :alert="true"
                       show-size-changer show-quick-jumper show-pagination="auto"
              >
                <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
                <span slot="position" slot-scope="text">
                  <ellipsis :length="30" tooltip>{{ text }}</ellipsis>
                </span>
                <span slot="subcontractorsType" slot-scope="text">
                  <ellipsis :length="30" tooltip>{{ text }}</ellipsis>
                </span>
                <span slot="action" slot-scope="text, record">
                  <MoreButton>
                    <a v-btnPermission="'pm_016_loo_02'" @click="handleSub(record, 'view')">查看</a>

                    <a-divider v-if="record.statusCode == '0' || record.statusCode == '3'"
                               v-btnPermission="'pm_016_loo_02'" type="vertical"
                    />
                    <a v-if="record.statusCode == '0' || record.statusCode == '3'" v-btnPermission="'pm_016_edi_03'"
                       @click="handleEdit(record, 'edit')"
                    >编辑</a>

                    <a-divider v-if="record.statusCode == '4' && record.button" v-btnPermission="'hse_037_yhx_06'"
                               type="vertical"
                    />
                    <a v-if="record.statusCode == '4' && record.button" v-btnPermission="'hse_037_yhx_06'"
                       @click="handleEdit(record,'kaocha')"
                    >安全考察</a>
<!--                    <a @click="handleEdit(record,'kaocha')">安全考察</a>-->

                    <a-divider v-if="record.statusCode == '1' || record.statusCode == '2' || record.statusCode == '3'"
                               v-btnPermission="'pm_016_moi_05'" type="vertical"
                    />
                    <a v-if="record.statusCode == '1' || record.statusCode == '2' || record.statusCode == '3'"
                       v-btnPermission="'pm_016_moi_05'" @click="handleApproveDetail(record)"
                    >审批信息</a>
                    <a v-if="record.statusCode == '0'" v-btnPermission="'pm_016_del_05'" @click="handleDelete(record)">删除</a>
                    <!-- 审核通过才能打印 -->
                    <BasePrintButton v-if="record.statusCode == '2'" :before-print-fn="getPrintData(record)"
                                     button-type="a"
                    />
                    <!--<approval-button  :status="record.statusCode" :id="record.surveyId" v-btnPermission="'pm_015_moi_05'"></approval-button>-->
                  </MoreButton>
                </span>
              </list-table>
            </a-col>
          </a-row>
        </div>
        <!-- 审批详情 -->
        <ApprovalDetails :title="'审批详情'" :business-id="businessId" :visible="visibleApprove"
                         @cancel="visibleApprove = false" @confirm="visibleApprove = false"
        />
      </a-card>
    </a-spin>
  </base-breadcrumb>
</template>

<script>
import BasePrintButton from '@/components/BasePrintButton' // 打印按钮
import { STable, Ellipsis } from '@/components'
import {
  getSubcontractorsInvestigate,
  addbasicInformation,
  updateSubStatus,
  deleteSub,
  inspectPrint
} from '@/api/subcontractors/subcontractors'
import { auditStatus } from '@/components/constant'
import recordVue from '@/views/project/maintain/record.vue'
import ApprovalDetails from '@/components/approvalDetails'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import api from '@/api/project/typeMaintenance' // 分公司选择框
const columns = [
  {
    title: '序号',
    width: 65,
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '考察分包商',
    width: 100,
    dataIndex: 'subcontractorName'
  },
  {
    title: '考察组组长',
    width: 100,
    dataIndex: 'investigateLeader'
  },
  {
    title: '职位',
    dataIndex: 'position',
    width: 100,
    scopedSlots: { customRender: 'position' }
  },
  {
    title: '所属分公司',
    width: 100,
    dataIndex: 'branchCompany'
  },
  {
    title: '分包商类型',
    width: 100,
    dataIndex: 'subcontractorsType',
    scopedSlots: { customRender: 'subcontractorsType' }
  },
  {
    title: '考察类型',
    width: 100,
    dataIndex: 'investigateType'
  },
  {
    title: '状态 ',
    width: 100,
    dataIndex: 'status'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 200,
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'a' + Date.now(),
  components: {
    STable,
    ApprovalDetails,
    Ellipsis,
    BasePrintButton
  },
  data() {
    this.columns = columns
    this.auditStatus = auditStatus
    return {
      searchArr: [
        // 搜索框
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '分公司',
        //   valueKey: 'branchComId',
        //   type: SelectSubCompany
        // },
        {
          name: '分包商名称',
          value: 'subcontractorName',
          type: 'input'
        },
        {
          name: '分包商类型',
          value: 'typeId',
          type: 'tree-select',
          attrs: {
            allowClear: true,
            replaceFields: { children: 'children', title: 'name', key: 'id', value: 'id' }
          },
          data: []
        },
        {
          name: '考察类型',
          value: 'investigateType',
          type: 'select',
          data: [
            { value: '0', label: '服务类分包商考察' },
            { value: '1', label: '甲指分包商考察' },
            { value: '2', label: '劳务/专业分包商考察' }
          ]
        },
        {
          name: '状态',
          value: 'status',
          type: 'select',
          data: [
            { value: '0', label: '待提交审核' },
            { value: '1', label: '审核中' },
            { value: '2', label: '审核通过' },
            { value: '3', label: '审核不通过' },
            { value: '4', label: '安全考察中' }
          ]
        }
      ],
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getSubcontractorsInvestigate(requestParameters).then(res => {
          this.total = res.totalSize
          return res
        })
      },
      selectedRowKeys: '',
      progressing: false,
      selectedRows: '',
      confirmLoading: false,
      visible: false,
      id: '',
      total: '',
      businessId: '',
      visibleApprove: false
    }
  },
  created() {
    this.getTreeData()
  },
  methods: {
    async getTreeData() {
      let res = await api.labelTreeList()
      if (res.code === 200) {
        this.searchArr[2].data = this.allEnabledDigui(res.data)
      }
    },
    allEnabledDigui(list) {
      let r = []
      list.map(v => {
        v.disabled = false
        if (v.children && v.children.length) {
          v.children = this.allEnabledDigui(v.children)
        }
        r.push(v)
      })
      return r
    },
    getPrintData(data) {
      return () => {
        return inspectPrint({ surveyId: data.surveyId }).then(res => {
          return res.data?.content
        })
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      console.log(selectedRowKeys)
      console.log(selectedRows)
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleDelete(record) {
      let _this = this
      this.$confirm({
        title: '警告提醒！',
        content: '是否删除当前数据？',
        okType: 'danger',
        onOk() {
          deleteSub({ id: record.surveyId }).then(res => {
            if (res.code == 200 || res.data.code == 200) {
              _this.$message.success('删除成功')
              _this.$refs.table.refresh(true)
            }
          })
        }
      })
    },
    search(values) {
      this.queryParam = values
      this.$refs.table.refresh(true)
    },
    handleApproveDetail(record) {
      this.visibleApprove = true
      this.businessId = record.surveyId
    },
    goAudit(record) {
      let _this = this
      this.$confirm({
        title: '提醒！',
        content: '是否要提交审核？',
        okType: 'danger',
        onOk() {
          _this.progressing = true
          updateSubStatus({ id: record.surveyId })
            .then(res => {
              if ((res.code = '200')) {
                _this.$message.success('提交成功')
                _this.progressing = false
                _this.$refs.table.refresh(true)
              }
            })
            .catch(() => {
              _this.progressing = false
            })
        },
        onCancel() {
          return false
        }
      })
    },
    goAdd(type) {
      this.$router.push({
        path: '/project/subcontractors/investigate/add',
        query: {
          type: type
        }
      })
    },
    handleSub(record, type) {
      this.$router.push({
        path: '/project/subcontractors/investigate/view',
        query: {
          id: record.surveyId,
          type: type
        }
      })
    },
    handleEdit(record, type) {
      this.$router.push({
        path: '/project/subcontractors/investigate/edit',
        query: {
          id: record.surveyId,
          type: type
        }
      })
    },
    handleOk() {
      this.visible = false
    },
    handleCancel() {
      this.visible = false
    }
  }
}
</script>

<style scoped>
.input {
  width: 100%;
}
</style>
